package ian.swing;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import org.jbpm.api.*;
import org.jbpm.api.task.Task;

public class ControlPanel extends JPanel implements MouseListener{
	
	private String user = "";
	private JPanel instancePanel = new JPanel();
	private JTable instanceTable = new JTable();
	private JPanel taskPanel = new JPanel();
	private JTable taskTable = new JTable();
	private JPanel definitionPanel = new JPanel();
	private JTable definitionTable = new JTable();
	
	private MyProcessEngine mpe = MyProcessEngine.getProcessEngineInstance();
	private ListTableModel instanceModel;
	private ListTableModel taskModel;
	private ListTableModel definitionModel;
	
	public ControlPanel(String u){
		super();
		this.user = u;
		this.setLayout(new GridLayout(3, 1));
		this.add(definitionPanel);
		this.add(instancePanel);
		this.add(taskPanel);
		definitionPanel.setBorder(new TitledBorder("Definitions"));
		definitionPanel.setLayout(new GridLayout(1,1));
		instancePanel.setBorder(new TitledBorder("Process Instances"));
		instancePanel.setLayout(new GridLayout(1,1));
		taskPanel.setBorder(new TitledBorder("Todo Tasks"));
		taskPanel.setLayout(new GridLayout(1,1));
		definitionPanel.add(definitionTable);
		instancePanel.add(instanceTable);
		taskPanel.add(taskTable);
		initTables();
	}
	
	private ListTableModel getModel(String type)
	{
		if(type.equals("instance")){
			if(instanceModel == null){
				String[] cn = {"Instance ID","Activity","State"};
				instanceModel = new ListTableModel(Arrays.asList(cn));
			}
			return instanceModel;
		}
		else if(type.equals("definition")){
			if(definitionModel == null){
				String[] cn = {"Instance ID","Name","Version","Key"};
				definitionModel = new ListTableModel(Arrays.asList(cn));
			}
			return definitionModel;
		}
		else{
			if(taskModel == null){
				String[] cn = {"Task ID","Name","Applicant"};
				taskModel = new ListTableModel(Arrays.asList(cn));
			}
			return taskModel;
		}
	}
	
	private void fillTable(List l, ListTableModel model, String type)
	{
		if(type == "instance")
			for(ProcessInstance pi : (List<ProcessInstance>)l){
				Object[] r = {pi.getId(),pi.findActiveActivityNames().toString(),pi.getState()};
				model.addRow(r);
			}
		else if(type == "definition")
			for(ProcessDefinition pi : (List<ProcessDefinition>)l){
				Object[] r = {pi.getId(),pi.getName(),pi.getVersion(),pi.getKey()};
				model.addRow(r);
			}
		else 
			for(Task pi : (List<Task>)l){
				Object[] r = {pi.getId(),pi.getName(),pi.getAssignee()};
				model.addRow(r);
			}
	}

	private void initTables(){
		definitionTable.setModel(getModel("definition"));
		fillTable(mpe.getDefinitionList(), getModel("definition"),"definition");
		instanceTable.setModel(getModel("instance"));
		fillTable(mpe.getInstanceList(), getModel("instance"), "instance");
		taskTable.setModel(getModel("task"));
		fillTable(mpe.getTaskList(user), getModel("task"), "task");
		definitionTable.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseExited(MouseEvent e) {
			}
			public void mouseEntered(MouseEvent e) {
			}
			public void mouseClicked(MouseEvent e) {
				mpe.startProcess(user);
				JOptionPane.showMessageDialog(definitionPanel.getParent(), "Process Started!");
			}
		});
		taskTable.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
			}
			public void mousePressed(MouseEvent e) {
			}
			public void mouseExited(MouseEvent e) {
			}
			public void mouseEntered(MouseEvent e) {
			}
			public void mouseClicked(MouseEvent e) {
				String taskId = (String)taskTable.getModel().getValueAt(taskTable.rowAtPoint(e.getPoint()),0);
				if(!user.equals("manager")){
					//mpe.submitTask(taskId, 5, "Default");
					//JOptionPane.showMessageDialog(definitionPanel.getParent(), "Application Sent!");
					FormFrame ff = new FormFrame(taskId, false);
					ff.setVisible(true);
					ff.setSize(200, 100);
				}
				else{
					//mpe.examineTask(true, taskId);
					//JOptionPane.showMessageDialog(definitionPanel.getParent(), "Application Approved!");
					FormFrame ff = new FormFrame(taskId, true);
					ff.setVisible(true);
					ff.setSize(200, 100);
				}
			}
		});
	}

	public void mouseClicked(MouseEvent e) {
		
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	public void mousePressed(MouseEvent e) {
	}
	public void mouseReleased(MouseEvent e) {
	}
	
}
